/*
* @Author: root
* @Date:   2017-11-14 13:15:15
* @Last Modified by:   root
* @Last Modified time: 2017-11-14 22:38:58
*/
'user strict'
require('./index.css');
require('page/common/nav-simple/index.js');
var _user  = require('service/user-service.js');
var _utils = require('util/util.js');
//表单的错误提示
var formError = {
	show : function(errMsg){
		$('.error-item').show().find('.err-msg').text(errMsg);
	},
	hide : function(){
		$('.error-item').hide().find('.err-msg').text('');
	}
}
var page = {
	data : {
		username : '',
		question : '',
		answer   : '',
		token	 : ''
	},
	init : function(){
		this.onload();
		this.bindEvent();
	},
	onload : function(){
		this.loadStepUsername();
	},
	bindEvent : function(){
		var _this = this;
		//输入用户名下一步的点击
		$('#submit-username').click(function(){
			var username = $.trim($('#username').val());
			//用户名存在
			if(username){
				_user.getQuestion(username,function(res){
					_this.data.username = username;
					_this.data.question = res;
					_this.loadStepQuestion();
				},function(errMsg){
                    formError.show(errMsg);
                });
			}//用户名不存在
			else {
				formError.show("请输入用户名");
			}
		});
		//输入问题提示问题答案中的按钮的点击
		$('#submit-question').click(function(){
			var answer = $.trim($('#answer').val());
			//检查答案是否存在
			if(answer){ 
				_user.checkAnswer({
					username : _this.data.username,
					question : _this.data.question,
					answer   : answer
				},function(res){
					_this.data.answer = username;
					_this.data.token  = res;
					_this.loadStepPassword();
				},function(errMsg){
                    formError.show(errMsg);
                });
			}//用户名不存在
			else {
				formError.show("请输入密码提示的答案 ");
			}
		});
		//输入问题提示问题答案中的按钮的点击
		$('#submit-question').click(function(){
			var answer = $.trim($('#answer').val());
			//检查答案是否存在
			if(answer){ 
				_user.checkAnswer({
					username : _this.data.username,
					question : _this.data.question,
					answer   : answer
				},function(res){
					_this.data.answer = username;
					_this.data.token  = res;
					_this.loadStepPassword();
				},function(errMsg){
                    formError.show(errMsg);
                });
			}//用户名不存在
			else {
				formError.show("请输入密码提示的答案 ");
			}
		});
		//输入新密码后
		$('#submit-password').click(function(){
			var password = $.trim($('#password').val());
			//密码是否为空
			if(password && password.length>= 6){ 
				_user.reserPassword({
					username 	  : _this.data.username,
					passwordNew   : password,
					forgetToken   : _this.data.token
				},function(res){
					window.location.href = './result.html?type=pass-reset';
				},function(errMsg){
                    formError.show(errMsg);
                });
			}//密码为空
			else {
				formError.show("请输入不小于6位的密码 ");
			}
		});
	},
	//加载输入用户名的一步
	loadStepUsername : function(){
		$('.step-username').show();
	},
	//加载输入问题答案的一步
	loadStepQuestion : function(){
		//清除错误提示
		formError.hide();
		//做容器的切换

		$('.step-username').hide()
            .siblings('.step-question').show().find('.question').text(this.data.question);
	},
	//加载输入用户新密码的一步
	loadStepPassword : function(){
		formError.hide();
		$('.step-question').hide().siblings('.step-password').show();
	},

};
$(function(){
	page.init();
})